package com.glink.number.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;

/**
 * @author xum
 */
@Controller
@Slf4j
public class AuthController {
    //参数配置文件
    public static final String TMPL = "96669784094056891583095000111622560";

    @GetMapping("/getSerialNumber")
    @ResponseBody
    public String getSerialNumber() {
        String serialNumber = null;
        try {
            // 获取设备序号
            serialNumber = serialNumber();
            log.info("磁盘序列号为：{}", serialNumber);
        }catch (Exception e){
            log.error("运行出错");
        }
        
        return serialNumber;
    }

    /**
     * 获取序号
     *
     * @return 序号
     */
    public static String serialNumber() {
        String property = System.getProperty("os.name").toLowerCase();
        String serialNumber = null;
        if (property.contains("windows")) {
            serialNumber = getHardDiskSerialNumber();
        } else if (property.contains("linux")) {
            serialNumber = deviceCid();
        }
        return serialNumber;
    }


    /**
     * 获取 硬盘序列号(Windows)
     *
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static String getHardDiskSerialNumber() {
        String result = "";
        try {
            File file = File.createTempFile("realhowto", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new FileWriter(file);

            String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                    + "Set colDrives = objFSO.Drives\n"
                    + "Set objDrive = colDrives.item(\"C\")\n"
                    + "Wscript.Echo objDrive.SerialNumber"; // see note
            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec(
                    "cscript //NoLogo " + file.getPath());
            BufferedReader input = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.trim();
    }

    public static String deviceCid() {
        try {
            String serialNumber = CommandUtils.exeCommand("sudo cat /sys/block/mmcblk0/device/cid ");
            if (StringUtils.isNotBlank(serialNumber)&&serialNumber.length()>10) {
                return serialNumber;
            }
        } catch (Exception e) {
            log.error("获取序列号1出错");
        }
        try {
            String serialNumber = CommandUtils.exeCommand("sudo cat /sys/devices/virtual/dmi/id/product_uuid ");
            if (StringUtils.isNotBlank(serialNumber)&&serialNumber.length()>10) {
                return serialNumber;
            }
        } catch (Exception e) {
            log.error("获取序列号2出错");
        }
        try {
            String serialNumber = CommandUtils.exeCommand("sudo cat /sys/firmware/devicetree/base/serial-number ");
            if (StringUtils.isNotBlank(serialNumber)&&serialNumber.length()>10) {
                return serialNumber;
            }
        } catch (Exception e) {
            log.error("获取序列号3出错");
        }
        return TMPL;
    }
}
